/*
  FAT32 files.
  Created on 2006-12-20 by ZZP
*/
/*
  Revision History :
  
  2007.10.15 PCB1.6, SoftVer2.0
    1. Support CF card up to 4G
	2. Not use malloc/free anymore
	3. Add JWData sectors to CF
*/

#include "DSP281x_Device.h"
#include "GPS_SatelliteData.h"




//////////////////////////////////////////////////////////
//   Local function prototypes
//////////////////////////////////////////////////////////
void ReadDisk(unsigned int Disk,unsigned int *Dest,
              unsigned long PhysicalSec,unsigned int SecCnt);

unsigned int WriteDisk(unsigned int Disk,unsigned int *Src,
               unsigned long PhysicalSec,unsigned int SecCnt);





////////////////////////////////////////
//       Function realization
////////////////////////////////////////
signed int FAT32_GetDiskInfo(unsigned int Disk, 
                             DISK_INFO *Info,
                             BPB_INFO *bpb)
{
  
  return (1);
}

unsigned int FAT32_SetFSInfo(unsigned int Disk,
                             BPB_INFO *bpb)
{
  
  return(1);
}

unsigned int FAT32_GetRootData(unsigned int Disk,
                               DISK_INFO *Info,
                               BPB_INFO *bpb)
{
  
  return(0);
}


unsigned long FAT32_FormatDisk(unsigned int Disk,
                               DISK_INFO *Info,
                               BPB_INFO *bpb,
                               DATE_TIME_INFO *dt,
                               unsigned int FormatAll)
{
  
  return (1);
}

unsigned long FAT32_CreateRootDir(unsigned int Disk,
                                  BPB_INFO *bpb, DATE_TIME_INFO *dt)
{
  
   
  return(1);
}

Uns FAT32_CreateBigFile(Uns Disk, BPB_INFO *bpb, DATE_TIME_INFO *dt)
{
  
  return (1);
}

Uns FAT32_CheckBigFile(Uns *Buf, BPB_INFO *bpb)
{
   
  return (1);
}



void ReadDisk(unsigned int Disk,unsigned int *Dest,
              unsigned long PhysicalSec,unsigned int SecCnt)
{
  if (Disk == HOST_CF) //CF card
    ;//ReadCF(Dest,PhysicalSec,SecCnt);
  else // Hard disk
  {
    //USB Commands here
    MB_ReadCF(Dest, PhysicalSec, SecCnt);
    //TestUnitCmd();
  }
}

unsigned int WriteDisk(unsigned int Disk,unsigned int *Src,
               unsigned long PhysicalSec,unsigned int SecCnt)
{
  unsigned int Res = 0;
  
  if (Disk == HOST_CF) //CF card
    ;//WriteCF(Src,PhysicalSec,SecCnt);
  else //Hard disk
  {
    //USB commands here
    //TestUnitCmd();
	if (Hc_XDevInfo.DevOK)
    {
      Res = MB_WriteCF(Src, PhysicalSec, SecCnt);
	  if (Res == 2)//Err
	    Hc_XDevInfo.DevOK = 0;
	}
  }
  return (Res);
}

Uns FAT32_GetFileClusters(Uns Disk, BPB_INFO *bpb, LgUns *Dest, LgUns FirstCluster)
{
  LgUns DirClus;
  Uns Cnt;
  LgUns CurrClus, NxtClus, CurrFATEntOffset, NxtFATEntOffset, FATOffset;
  LgUns CurrSec, NxtSec;
  
  
  DirClus = FirstCluster;
  NxtClus = 0;
  CurrClus = DirClus;
  Dest[0] = DirClus;
  Cnt = 1;

  FATOffset = (CurrClus << 2);
  CurrFATEntOffset = ((FATOffset % bpb->BytesPerSec) >> 1);
  CurrSec = bpb->RsvdSecCnt + (FATOffset / bpb->BytesPerSec);
  ReadDisk(Disk, CF_Buf, CurrSec, 1);
  NxtClus = CF_Buf[CurrFATEntOffset] + ((LgUns)CF_Buf[CurrFATEntOffset + 1] << 16);
  NxtClus &= 0x0FFFFFFF;
  while (NxtClus != FAT_EOC)
  {
    if ((NxtClus < 3) || (NxtClus > bpb->CountOfClusters + 2))
      return (Cnt);
    
    Dest[Cnt] = NxtClus;
    Cnt++;
    FATOffset = (NxtClus << 2);
    NxtFATEntOffset = ((FATOffset % bpb->BytesPerSec) >> 1);
    NxtSec = bpb->RsvdSecCnt + (FATOffset / bpb->BytesPerSec);
    if (NxtSec != CurrSec)
    {
      ReadDisk(Disk, CF_Buf, NxtSec, 1);
      CurrSec = NxtSec;
    }
    NxtClus = CF_Buf[NxtFATEntOffset] + ((LgUns)CF_Buf[NxtFATEntOffset + 1] << 16);
    NxtClus &= 0x0FFFFFFF;
  }

  return (Cnt);
}















